package com.app.base.service.core.employees.service.impl;

import com.app.base.model.dto.EmpListDto;
import com.app.base.model.dto.HandleInductionDto;
import com.app.base.utlis.genericity.GenericParadigm;
import com.app.base.model.vo.employees.EmployeesListQueryVo;
import com.app.base.model.vo.employees.PostTransferVo;
import com.app.base.model.vo.UsCoVo;
import com.app.base.model.vo.employees.RemarksVo;
import com.app.base.service.auth.service.SysUserService;
import com.app.base.service.core.company.entity.Company;
import com.app.base.service.core.company.service.CompanyService;
import com.app.base.service.core.employees.entity.EmpDetail;
import com.app.base.service.core.employees.entity.EmpFile;
import com.app.base.service.core.employees.mapper.EmpDetailMapper;
import com.app.base.service.core.employees.service.EmpDetailService;
import com.app.base.service.core.employees.service.EmpFileService;
import com.app.base.service.core.personnel.entity.PersonInfo;
import com.app.base.service.core.personnel.service.PersonInfoService;
import com.app.base.utlis.email.EmailUtils;
import com.app.base.utlis.email.Mail;
import com.app.base.utlis.result.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 员工信息 服务实现类
 * @author yuhag
 * @since 2022-05-11
 */
@Service
@Slf4j
public class EmpDetailServiceImpl extends ServiceImpl<EmpDetailMapper, EmpDetail> implements EmpDetailService {

    @Resource
    private SysUserService userService;

    @Resource
    private EmpFileService fileService;

    @Resource
    private PersonInfoService personInfoService;

    @Resource
    private CompanyService companyService;

    @Resource
    private EmailUtils emailUtils;


    @Transactional
    @Override
    public Result handleInduction(HttpServletRequest request, HandleInductionDto handleInductionDto) {
        try{
            UsCoVo info = userService.getUserCompanyInfo(request);
            String companyId = info.getCompanyId();
            Company company = companyService.getById(companyId);
            String abbreviation = company.getAbbreviation();
            String code = abbreviation + new SimpleDateFormat("yyyyMMddHHmm").format(new Date());

            String infoId = handleInductionDto.getInfoId();
            PersonInfo personInfo = new PersonInfo();
            personInfo.setId(infoId);
            personInfo.setStatus(1);
            personInfoService.updateById(personInfo);

            EmpDetail empDetail = new EmpDetail();
            BeanUtils.copyProperties(handleInductionDto, empDetail);
            empDetail.setCode(code);
            empDetail.setCompanyId(info.getCompanyId());
            empDetail.setCreatedBy(info.getUserId());
            empDetail.setModifyBy(info.getUserId());
            empDetail.setStatus(1);
            baseMapper.insert(empDetail);
            String empId = empDetail.getId();

            handleInductionDto.getFileList().forEach(item->{

                EmpFile empFile = new EmpFile();
                BeanUtils.copyProperties(item, empFile);
                empFile.setCode(empFile.getCode().trim());
                empFile.setEmpId(empId);
                empFile.setCompanyId(info.getCompanyId());
                empFile.setCreatedBy(info.getUserId());
                empFile.setModifyBy(info.getUserId());
                fileService.save(empFile);
            });

            return Result.ok().message("添加成功！");
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result empSearchList(Long current, Long pageSize, EmployeesListQueryVo employeesListQueryVo) {
        try{
            Page<EmpListDto> page = new Page<>(current,pageSize);
            List<EmpListDto> details = baseMapper.pageEmpSearch(page,employeesListQueryVo);
            page.setRecords(details);
            return Result.ok(page);
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result notice(Mail mail) {
        try{
            String id = mail.getId();
            EmpDetail empDetail = baseMapper.selectById(id);
            String infoId = empDetail.getInfoId();
            PersonInfo personInfo = personInfoService.getById(infoId);
            String email = personInfo.getEmail();
            mail.setTo(email);
            emailUtils.htmlEmail(mail);
            return Result.ok().message("邮件放送成功");
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result postTransfer(PostTransferVo postTransferVo) {
        try{
            GenericParadigm<PostTransferVo> genericParadigm = new GenericParadigm<>();
            EmpDetail empDetail = genericParadigm.editEmp(postTransferVo);
            baseMapper.updateById(empDetail);
            return Result.ok().message("调岗成功");
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result editRemarks(RemarksVo remarksVo) {
        try{
            GenericParadigm<RemarksVo> genericParadigm = new GenericParadigm<>();
            EmpDetail empDetail = genericParadigm.editEmp(remarksVo);
            baseMapper.updateById(empDetail);
            return Result.ok().message("备注更新成功");
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}
